<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-L-admin1.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="shortcut icon" href="../images/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/xadmin.css">
    <script src="../js/jquery.min.js"></script>
    <script type="text/javascript" src="../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <script src="../js/checkbox.js"></script>
    <script src="../js/queryByName.js"></script>
</head>

<body class="layui-anim layui-anim-up">
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">导航</a>
        <a>
          <cite>实验室信息</cite></a>
      </span>
    <a class="layui-btn layui-btn-primary layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:38px">ဂ</i></a>
</div>
<div class="x-body">
    <div class="layui-row">
        <div class="layui-form layui-col-md12 x-so">
            <label for="labid"></label>
            <input type="text" id="labid" placeholder="请输入实验室id" autocomplete="off" class="layui-input">
            <button onclick="queryLabById()" class="layui-btn"><i class="layui-icon">&#xe615;</i></button>
            <input type="text" id="labname" placeholder="请输入实验室名称" autocomplete="off" class="layui-input">
            <button onclick="queryLabByName()" class="layui-btn"><i class="layui-icon">&#xe615;</i></button>
        </div>
    </div>
    <xblock>
        <button class="layui-btn layui-btn-danger" onclick="lab_delSome()"><i class="layui-icon"></i>批量删除</button>
        <button class="layui-btn" onclick="x_admin_show('添加实验室','laboratory-add.html')"><i
                class="layui-icon"></i>添加
        </button>
        <button class="layui-btn" onclick="x_admin_show('实验室开放时间','LabTable-time.html')">实验室开放时间</button>
    </xblock>
    <table class="layui-table">
        <thead>
        <tr>
            <th>
                <label>
                    <input type="checkbox" onclick="checkAll(this)"/>
                </label>
            </th>
            <th>ID</th>
            <th>实验室名称</th>
            <th>地址</th>
            <th>座位数</th>
            <th>备注</th>
            <th>所属院系</th>
            <th>类别</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="labs"></tbody>
    </table>

    <!--      <div class="page">-->
    <!--        <div>-->
    <!--          <a class="prev" href="">&lt;&lt;</a>-->
    <!--          <a class="num" href="">1</a>-->
    <!--          <span class="current">2</span>-->
    <!--          <a class="num" href="">3</a>-->
    <!--          <a class="num" href="">489</a>-->
    <!--          <a class="next" href="">&gt;&gt;</a>-->
    <!--        </div>-->
    <!--      </div>-->

</div>
<script>
    //判断用户是否登录
    $(function () {
        $.ajax({
            type: "post",
            url: "http://localhost:8080/user/getCurrentUser",
            success: function (result) {
                if (result.code == '200' && result.object.priority == '2') {
                    findAllLabs();
                } else {
                    alert("请先登录账号！");
                    window.location.href = "../../templates/login.html";
                }
            }
        });
    });

    //局部刷新全部实验室信息
    function findAllLabs() {
        $.post('http://localhost:8080/lab/findAllLabs', function (result) {
            let res = result.object;
            let str = "";
            if (result.code == '200') {
                for (let i = 0; i < res.length; i++) {
                    str += "<tr><td>\n" +
                        "<input type='checkbox' name='checkbox' value='" + res[i].labid + "'/>\n" +
                        "</td>";
                    str += "<td>" + res[i].labid + "</td>";
                    str += "<td>" + res[i].labname + "</td>";
                    str += "<td>" + res[i].address + "</td>";
                    str += "<td>" + res[i].seats + "</td>";
                    str += "<td>" + res[i].remark + "</td>";
                    str += "<td>" + res[i].faculty + "</td>";
                    if (res[i].role == 0) {
                        str += "<td>公用实验室</td>";
                    } else if (res[i].role == 1) {
                        str += "<td>专用实验室</td>";
                    } else {
                        alert("出现错误");
                    }
                    str += "<td class=\"td-manage\">\n" +
                        "<a title=\"编辑\"  onclick=\"user_edit_show('编辑','laboratory-edit.html'," + res[i].labid + ")\" href=\"javascript:;\">\n" +
                        "<i class=\"layui-icon\">&#xe642;</i>\n" +
                        "</a>\n" +
                        "<a title=\"删除\" onclick=\"lab_del(this," + res[i].labid + ")\" href=\"javascript:;\">\n" +
                        "<i class=\"layui-icon\">&#xe640;</i>\n" +
                        "</a>\n" +
                        "<a title=\"预约\" onclick='lab_book(\"预约\",\"../../templates/bookLab.html\"," + res[i].labid + ",\"" + res[i].labname + "\")' href=\"javascript:;\">\n" +
                        "<img src='../images/book.png' width='20' height='20' alt='预约'/>\n" +
                        "</a>\n" +
                        "</td></tr>";
                    $("#labs").html(str);
                }
            } else {
                alert("请求失败")
            }
        });
    }

    //根据实验室id局部刷新实验室信息
    function queryLabById() {
        let labid=$("#labid").val();
        let re = /[1-9][0-9]*/;//判断字符串是否为数字//判断正整数/[1−9]+[0−9]∗]∗/
        if (!re.test(labid)) {
            alert("使用id查询请输入数字");
        }
        let data = {"labid": labid};
        $.post('http://localhost:8080/lab/findLabById', data, function (result) {
            let res = result.object;
            let str = "";
            if (result.code == '200') {
                str += "<tr><td>\n" +
                    "<input type='checkbox' name='checkbox' value='" + res.labid + "'/>\n" +
                    "</td>";
                str += "<td>" + res.labid + "</td>";
                str += "<td>" + res.labname + "</td>";
                str += "<td>" + res.address + "</td>";
                str += "<td>" + res.seats + "</td>";
                str += "<td>" + res.remark + "</td>";
                str += "<td>" + res.faculty + "</td>";
                if (res.role == 0) {
                    str += "<td>公用实验室</td>";
                } else if (res.role == 1) {
                    str += "<td>专用实验室</td>";
                } else {
                    alert("出现错误");
                }
                str += "<td class=\"td-manage\">\n" +
                    "<a title=\"编辑\"  onclick=\"user_edit_show('编辑','laboratory-edit.html'," + res.labid + ")\" href=\"javascript:;\">\n" +
                    "<i class=\"layui-icon\">&#xe642;</i>\n" +
                    "</a>\n" +
                    "<a title=\"删除\" onclick=\"lab_del(this," + res.labid + ")\" href=\"javascript:;\">\n" +
                    "<i class=\"layui-icon\">&#xe640;</i>\n" +
                    "</a>\n" +
                    "<a title=\"预约\" onclick='lab_book(\"预约\",\"../../templates/bookLab.html\"," + res.labid + ",\"" + res.labname + "\")' href=\"javascript:;\">\n" +
                    "<img src='../images/book.png' width='20' height='20' alt='预约'/>\n" +
                    "</a>\n" +
                    "</td></tr>";
                $("#labs").html(str);
            } else {
                alert("请求失败")
            }
        });
    }

    /*用户-删除*/
    function lab_del(obj, labid) {
        layer.confirm('确认要删除吗？', function (index) {
            //发异步删除数据
            $.ajax({
                type: "delete",
                url: "http://localhost:8080/lab/deleteLabById",
                data: "labid=" + labid,
                success: function (result) {
                    if (result.code = "200") {
                        //obj是属于js对象
                        //js对象转换成jquery对象
                        let tr = $(obj).parent().parent();
                        tr.remove();
                        layer.msg('已删除!', {icon: 1, time: 1000});
                    }
                }
            });
        });
    }

    //删除多个实验室
    function lab_delSome() {
        let checkboxes = document.getElementsByName("checkbox");
        let check_val = [];
        for (let k in checkboxes) {
            if (checkboxes[k].checked)
                check_val.push(checkboxes[k].value);
        }
        $.ajax({
            type: "delete",
            url: "http://localhost:8080/lab/deleteLabSome",
            data: "ids=" + check_val,
            success: function (result) {
                if (result.code = "200") {
                    //obj是属于js对象
                    //js对象转换成jquery对象
                    let trs = [];
                    for (let s = 0; s < checkboxes.length; s++) {
                        if (checkboxes[s].checked) {
                            let tr = checkboxes[s];
                            trs.push(tr);
                        }
                    }
                    for (let i = 0; i < trs.length; i++) {
                        let tr = $(trs[i]).parent().parent();
                        tr.remove();
                    }
                }
            }
        });
    }

    /*
    参数解释：
    title   标题
    url     请求的url
    id      需要操作的数据id
    w       弹出层宽度（缺省调默认值）
    h       弹出层高度（缺省调默认值）
*/
    function lab_book(title, url, id, labName, w, h) {
        if (title == null || title == '') {
            title = false;
        }
        if (url == null || url == '') {
            url = "404.html";
        }
        if (id == null || id == '') {
            alert("出错了！");
        } else {
            if (window.localStorage) {
                localStorage.labid = id;
            }
        }
        if (labName == null || labName == '') {
            alert("出错了！")
        } else {
            if (window.localStorage) {
                localStorage.labname = labName;
            }
        }
        if (!(w == null || w == '')) {
        } else {
            w = ($(window).width() * 0.9);
        }
        if (h == null || h == '') {
            h = ($(window).height() - 50);
        }

        layer.open({
            type: 2,
            area: [w + 'px', h + 'px'],
            fix: false, //不固定
            maxmin: true,
            shadeClose: true,
            shade: 0.4,
            title: title,
            content: url
        });
    }
</script>
</body>

</html>